package com.qf.fmall2202.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.entity.ProductSku;
import com.qf.fmall2202.product.service.ProductImgService;
import com.qf.fmall2202.product.service.ProductService;
import com.qf.fmall2202.product.service.ProductSkuService;
import com.qf.fmall2202.shopcart.entity.ShoppingCart;
import com.qf.fmall2202.shopcart.dao.ShoppingCartDao;
import com.qf.fmall2202.shopcart.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2202.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author 徐英海
 * @since 2022-11-08
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ProductSkuService productSkuService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductImgService productImgService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo queryList(Integer userId) {
        //根据userId查询该用户的购物车
        List<ShoppingCart> shoppingCartList = this.list(
                new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId, userId)
        );
        populateCart(shoppingCartList);//补全数据
        return ResultVo.ok("登陆成功", shoppingCartList);
    }

    private void populateCart(List<ShoppingCart> shoppingCartList) {
        //将购物车中的全部商品的skuId取出来
        List<String> skuIdList = shoppingCartList.stream()
                .map(ShoppingCart::getSkuId)
                .collect(Collectors.toList());
        //将购物车中的全部商品的productId取出来
        List<String> productIdList = shoppingCartList.stream()
                .map(ShoppingCart::getProductId)
                .collect(Collectors.toList());
        //创建根据skuId找ProductSku对象的Map
        Map<String, ProductSku> productSkuMap = productSkuService.list(
                        new LambdaQueryWrapper<ProductSku>()
                                .in(ProductSku::getSkuId, skuIdList)
                ).stream()
                .collect(Collectors.toMap(ProductSku::getSkuId, productSku -> productSku));
        //创建根据productId找Product对象的Map
        Map<String, Product> productMap = productService.list(
                        new LambdaQueryWrapper<Product>()
                                .in(Product::getProductId, productIdList)
                ).stream()
                .collect(Collectors.toMap(Product::getProductId, product -> product));
        //创建根据productId找对应的图片的Map
        Map<String, String> productImgMap = productImgService.list(
                        new LambdaQueryWrapper<ProductImg>()
                                .eq(ProductImg::getIsMain, 1)
                                .in(ProductImg::getItemId, productIdList)
                ).stream()
                .collect(Collectors.toMap(ProductImg::getItemId, ProductImg::getUrl));
        //将数据拼接到一起
        shoppingCartList.forEach(
                shoppingCart -> {
                    ProductSku productSku = productSkuMap.get(shoppingCart.getSkuId());
                    Product product = productMap.get(shoppingCart.getProductId());
                    String url = productImgMap.get(shoppingCart.getProductId());
                    shoppingCart.setProductName(product.getProductName());
                    shoppingCart.setProductImg(url);
                    shoppingCart.setSkuName(productSku.getSkuName());
                    shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
                    shoppingCart.setSellPrice(productSku.getSellPrice());
                    shoppingCart.setStock(productSku.getStock());
                }
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo addToShoppingCart(ShoppingCart shoppingCart) {
        shoppingCart.setCartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
        this.save(shoppingCart);
        return ResultVo.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo updateCartNumByCartId(Integer cartId, Integer cartNum) {
        this.update(
                new LambdaUpdateWrapper<ShoppingCart>()
                        .set(ShoppingCart::getCartNum, cartNum.toString())
                        .eq(ShoppingCart::getCartId, cartId)
        );
        return ResultVo.ok();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo listbycids(String cids) {
        List<ShoppingCart> shoppingCartList = splitCidsToList(cids);
        return ResultVo.ok("查询成功", shoppingCartList);
    }

    @Override
    public List<ShoppingCart> splitCidsToList(String cids) {
        String[] cidArr = cids.split(",");
        List<String> cidList = Arrays.asList(cidArr);
        List<ShoppingCart> shoppingCartList = this.list(new LambdaQueryWrapper<ShoppingCart>().in(ShoppingCart::getCartId, cidList));
        this.populateCart(shoppingCartList);
        return shoppingCartList;
    }
}
